package com.right.health.app.ui.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.AddressListResponse;
import com.right.health.app.dialog.AddressDeleteDialog;
import com.right.health.app.net.UserApi;
import com.right.health.app.utils.SoftInputUtil;

/**
 * @author: weming
 * @date: 2021/3/28
 * @description:编辑收货地址
 */
public class EditAddressActivity extends BaseActivity implements OnHttpListener {

    private static final int HTTP_CODE_DELETE = 1;
    private static final int HTTP_CODE_ADD = 2;
    private static final int HTTP_CODE_MODIFY = 3;


    private EditText mConsigneeEt, mPhoneEt, mAddressEt;
    private TextView mTitleTv, mDeleteTv, mAreaTv;
    private Button mConfirmBtn;
    private boolean mIsEdit;
    private UserApi mUserApi;
    private AddressListResponse.UserAddressBean mAddressBean;


    private int mProvinceId = -1, mCityId = -1, mAreaId = -1;
    private AddressDeleteDialog mDeleteDialog;

    private AllAreaUIUtil.ResultCallback mCallback = new AllAreaUIUtil.ResultCallback() {
        @Override
        public void onSelected(String content, int provinceId, int cityId, int areaId) {
            mAreaTv.setText(content);
            mProvinceId = provinceId;
            mCityId = cityId;
            mAreaId = areaId;
        }

        @Override
        public void onSearch(String content) {
            mAreaTv.setText(content);
        }
    };


    public static void goEdit(Activity context, AddressListResponse.UserAddressBean bean) {
        Intent intent = new Intent(context, EditAddressActivity.class);
        intent.putExtra("isEdit", true);
        intent.putExtra("data", bean);
        context.startActivityForResult(intent, 1000);
    }

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_edit_address);
    }

    @Override
    public void findView() {
        mConfirmBtn = findViewById(R.id.btn_confirm);
        mAreaTv = findViewById(R.id.tv_area);
        mDeleteTv = findViewById(R.id.tv_delete);
        mTitleTv = findViewById(R.id.tv_title);
        mConsigneeEt = findViewById(R.id.et_consignee);
        mPhoneEt = findViewById(R.id.et_phone);
        mAddressEt = findViewById(R.id.et_address);
        LinearLayout areaLy = findViewById(R.id.ly_area);

        areaLy.setOnClickListener(v -> {
            AllAreaUIUtil.getInstance().showDialog(this, mCallback);
        });

        mConfirmBtn.setOnClickListener(v -> confirm());
    }

    @Override
    public void initialize() {
        mIsEdit = getIntent().getBooleanExtra("isEdit", false);
        if (mIsEdit) {
            mDeleteTv.setVisibility(View.VISIBLE);
            mTitleTv.setText("编辑收货地址");
            mConfirmBtn.setText("确认修改");
            mAddressBean = (AddressListResponse.UserAddressBean) getIntent().getSerializableExtra("data");
            if (mAddressBean != null) {
                mConsigneeEt.setText(mAddressBean.getUsername());
                mConsigneeEt.setSelection(mAddressBean.getUsername().length());
                mPhoneEt.setText(mAddressBean.getUserphone());
                mAddressEt.setText(mAddressBean.getUseraddress());
                mDeleteTv.setVisibility(View.VISIBLE);
                mProvinceId=mAddressBean.getProvinceid();
                mCityId=mAddressBean.getCityid();
                mAreaId=mAddressBean.getCountyid();
                mDeleteTv.setOnClickListener(v -> showDeleteDialog());
                AllAreaUIUtil.getInstance().getContent(mAddressBean.getProvinceid(), mAddressBean.getCityid(), mAddressBean.getCountyid(), mCallback);
            } else {
                finish();
            }
        } else {
            if (AllAreaUIUtil.getInstance().isDataEmpty()) {
                AllAreaUIUtil.getInstance().getData(this, false);
            }
        }
        mUserApi = new UserApi();
        mUserApi.setListener(this);

        mConsigneeEt.requestFocus();
        mConsigneeEt.postDelayed(() -> SoftInputUtil.showSoftInputMode(this, mConsigneeEt), 200);
    }


    private void confirm() {
        SoftInputUtil.hideSoftInputMode(this, getCurrentFocus());
        String name = mConsigneeEt.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.show(this, "请输入收货人");
            return;
        }
        String phone = mPhoneEt.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.show(this, "请输入手机号码");
            return;
        } else if (!VerifyUtil.isMobile(phone)) {
            ToastUtil.show(this, R.string.please_enter_correct_phone);
            return;
        }
        if (TextUtils.isEmpty(mAreaTv.getText())) {
            ToastUtil.show(this, "请选择地区");
            return;
        }
        String address = mAddressEt.getText().toString();
        if (TextUtils.isEmpty(address)) {
            ToastUtil.show(this, "请输入详细地址");
            return;
        }
        if(mIsEdit) {
            mUserApi.addRequestCode(HTTP_CODE_MODIFY);
            mUserApi.modifyShippingAddress(mAddressBean.getAddressid(),name, phone, mProvinceId, mCityId, mAreaId,  address,mAddressBean.getIsdefault());
        }else {
            mUserApi.addRequestCode(HTTP_CODE_ADD);
            mUserApi.addShippingAddress(name, phone, mProvinceId, mCityId, mAreaId,  address);

        }
    }


    private void showDeleteDialog() {
        if (mDeleteDialog == null) {
            mDeleteDialog = new AddressDeleteDialog(this);
            mDeleteDialog.setDelegate(dialog -> {
                mUserApi.addRequestCode(HTTP_CODE_DELETE);
                mUserApi.deleteShippingAddress(mAddressBean.getAddressid());
                dialog.dismiss();
            });
        }
        mDeleteDialog.show();
    }


    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            String msg = "";
            if (request.requestCode == HTTP_CODE_ADD) {
                msg = "添加成功";
            } else if (request.requestCode == HTTP_CODE_MODIFY) {
                msg = "修改成功";
            } else if (request.requestCode == HTTP_CODE_DELETE) {
                msg = "删除成功";
            }
            ToastUtil.show(this, msg);
            Intent intent = new Intent();
            intent.putExtra("refresh", true);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

}
